<?php

namespace App\Http\Controllers\Common;

use Illuminate\Validation\Rule;
use Validator;
use App\Model\Users;
use function CircularHelper\failed;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class CircularController extends Controller
{
    /**
     * 展示用户通知
     *
     * Date: 2018/7/12
     * @author Leon
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $client = $request->get('client');
        $this->validate($request, [
            'client' => [
                'required',
                Rule::in([1, 2, 3, 4])
            ]
        ], [
            'client.required' => '平台不能为空',
            'client.in' => '平台参数不符合规定'
        ]);

        $data = Users::query()->where('id', Auth::user()->id)->with(['circular' => function ($query) use ($client) {
            if ($client == 2) {
                $query->whereIn('client', [1, 2, 3]);
            } elseif ($client == 4){
                $query->whereIn('client', [3, 4]);
            } else {
                $query->where('client', $client);
            }
        }])->get()[0]->toArray()['circular'];
        return success($data);
    }

    /**
     * 已读通知
     *
     * Date: 2018/7/12
     * @author Leon
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function read(Request $request)
    {
        $client = $request->get('client');
        $this->validate($request, [
            'client' => [
                'required',
                Rule::in([1, 2, 3, 4])
            ]
        ], [
            'client.required' => '平台不能为空',
            'client.in' => '平台参数不符合规定'
        ]);

        $data = Users::query()->where('id', Auth::user()->id)->with(['circular' => function ($query) use ($client) {
            if ($client == 2) {
                $query->whereIn('client', [1, 2, 3]);
            } elseif ($client == 4){
                $query->whereIn('client', [3, 4]);
            } else {
                $query->where('client', $client);
            }
        }])->get()[0]->toArray()['circular'];
        foreach ($data as $k => $v) {
            $circular_id[] = $v['id'];
        }

        if (!empty($circular_id)) {
            $user = Users::query()->find(Auth::user()->id);
            foreach ($circular_id as $k => $id) {
                $user->circular()->updateExistingPivot($id, ['read' => 1]);
            }
        }
        return success('已读');

    }

    /**
     * 删除通知
     *
     * Date: 2018/7/12
     * @author Leon
     * @param $client_id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($client_id)
    {
        $user = Users::query()->find(Auth::user()->id);
        $user->circular()->detach($client_id);
        return deleted();
    }

    /**
     * 未读通知个数
     *
     * Date: 2018/7/12
     * @author Leon
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function unread(Request $request)
    {
        $client = $request->get('client');
        $this->validate($request, [
            'client' => [
                'required',
                Rule::in([1, 2, 3, 4])
            ]
        ], [
            'client.required' => '平台不能为空',
            'client.in' => '平台参数不符合规定'
        ]);

        $data = Users::query()->where('id', Auth::user()->id)->with(['circular' => function ($query) use ($client) {
            if ($client == 2) {
                $query->whereIn('client', [1, 2, 3]);
            } elseif ($client == 4){
                $query->whereIn('client', [3, 4]);
            } else {
                $query->where('client', $client);
            }
        }])->count();
        return success($data);
    }
}
